package com.whl.sm.service.impl.approval;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whl.sm.common.result.Result;
import com.whl.sm.mapper.approval.VApprovalMapper;
import com.whl.sm.model.approval.VApproval;
import com.whl.sm.model.assets.ComUnits;
import com.whl.sm.model.notice.Attachment;
import com.whl.sm.model.system.Role;
import com.whl.sm.service.approval.VApprovalService;
import com.whl.sm.service.assets.ComUnitsService;
import com.whl.sm.service.notice.AttachmentService;
import com.whl.sm.service.repair.DataOperateLogService;
import com.whl.sm.service.system.OrgUserService;
import com.whl.sm.service.system.RoleService;
import com.whl.sm.sysEnum.CommonEnum;
import com.whl.sm.vo.ApprovalRoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuhaolun
 * @since 2022-01-12
 */
@Service
@Slf4j
public class VApprovalServiceImpl extends ServiceImpl<VApprovalMapper, VApproval> implements VApprovalService {

    @Autowired
    private OrgUserService orgUserService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AttachmentService attachmentService;

    @Autowired
    private DataOperateLogService dataOperateLogService;

    @Override
    public Result update(VApproval vApproval) {
        String currentId = orgUserService.getCurrentUserId();
        vApproval.setOperator(currentId);

        try{
            if (Objects.isNull(vApproval.getId())){
                vApproval.setApplicant(currentId);
                getBaseMapper().insert(vApproval);
            }else {
                getBaseMapper().updateById(vApproval);
            }
        }catch (Exception e){
            log.error("申请信息保存失败----->"+e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }

    @Override
    public VApproval getByUserId(String dataType) {
        VApproval rtValue  = null;
        String userId = orgUserService.getCurrentUserId();
        if (Objects.nonNull(userId)){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("applicant",userId);
            if (Objects.nonNull(dataType)){
                queryWrapper.eq("data_type",dataType);
            }
            queryWrapper.orderByDesc("update_time");
            List<VApproval> dataList = getBaseMapper().selectList(queryWrapper);
            rtValue = dataList.size()>0?dataList.get(0):null;
            List<Attachment> fileList = attachmentService.list(new QueryWrapper<Attachment>().eq("data_id ",rtValue.getId()));
            rtValue.setFileList(fileList);
        }
        return rtValue;
    }

    @Override
    public VApproval getByUserOfRole(String dataTpe) {
        VApproval resultData = this.getByUserId(dataTpe);
        if (Objects.nonNull(resultData)){
            Role linkRole = roleService.getById(resultData.getDataId());
            resultData.setRoleName(linkRole.getRoleName());
        }
        return resultData;
    }

    @Override
    public Result auditStatus(ApprovalRoleVo approvalRoleVo) {
        try {
            if (approvalRoleVo.getApprovalStatus() == CommonEnum.ApprovalStatus.PASS.getStep()){

            }
            VApproval target = getBaseMapper().selectById(approvalRoleVo.getId());
            target.setApprovalStatus(approvalRoleVo.getApprovalStatus());
            Result updateRt = update(target);
            if (!updateRt.isOk()){
                return updateRt;
            }
            Result recordLog = dataOperateLogService.addOperateLog(approvalRoleVo.getId(),"ROLE_APPROVAL",
                    "审核状态："+CommonEnum.ApprovalStatus.getValueByStep(approvalRoleVo.getApprovalStatus())+"  审核意见:"+approvalRoleVo.getOperateRemark());
            if (!recordLog.isOk()){
                return recordLog;
            }
        }catch (Exception e){
            log.error("角色申请审核失败------->"+e.getMessage());
        }
        return Result.ok();
    }

}
